package thread;

import java.util.concurrent.TimeUnit;

/**
 * @author Darkness Shadow
 * @description: java 多线程
 *  让听音乐和看新闻两个任务同时执行
 *  结果：只有看新闻这个任务一直在执行
 * @date 2020/1/4 18:43
 */
public class TryConcurrency {

    public static void main(String [] args){
        // 通过匿名内部类的方式创建线程，并且重写其中的run方法
        Thread thread = new Thread() {
            @Override
            public void run() {
                enjoyMusic();
            }
        };
        thread.start();
//        try {
//            TimeUnit.SECONDS.sleep(2);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        thread.start();
        browseNews();
    }

    private static void browseNews() {
        for (; ; ) {
            System.out.println("Uh-huh,the good nuews.");
            sleep(1);
        }
    }

    private static void enjoyMusic() {
        for (; ; ) {
            System.out.println("Uh-huh,the nice music");
            sleep(5);
        }
    }

    private static void sleep(int seconds) {
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
